4. Python learning (Think like a computer scientist): chapter 1&2
Homework:
Exercises from chapter 1&2 in (Think like a computer scientist) Submit your code to project
Learning outcomes -- Edited by Tom Zhou on June 1st
----About half of the things that we learned today are referred to the chapter one of the book "How to think like a computer scientist". To find out more, click me to see that chapter.
WikiSpace is the place where you show the result of your study, works, projects and activities. Everyone in our class can be the editor, and your efforts will make it more diverse and appealing to others.
To be a good computer scientist, you should:
Be able to solve problems
Be able to solve problems creatively
Be able to denote your thoughts using formal languages
Also we should be aware of the term "open source", which means that the code of the programes is published to all programmers in the world and you can change any part of it if you want. A significant benefit of open source is that others will not only have the chance of learning your code and algorithms, but also they can improve it bit by bit. In the class, we had better share our resources and workings in order to improve our abilities together.
The key things we learned today of programming may include:
--Introduction to Python as a programming language
--Definition of debugging and three errors that may occur
--Simple grammars involved in Python programming
--Some useful functions and modules in Python
As beginners, we should know that Python is an interpreted language. It is interpreted line by line, to a language that computers can understand and it runs immediately. By comparison, compiled languages such as VB and C++ will be only translated as a whole and it will not run right after translation.
There are three general types of errors that may occur during debugging. The first one is called “Syntax error”, which is resulted from incorrect notations or grammar. For example:
print"This is a great example"
If you execute the program, it is very likely to return an error simply because you missed the parentheses. The correct format should be:
print("This is the correct format")
Also please notice that the uppercase letters are very DIFFERENT from lowercase letters. Your Python interpreter will not recognize them if you type the word "Print" rather than the word "print".
The second type of errors are runtime errors. Their typical examples are the inappropriate operations between data types. For instance:
a ="Tom"
b =2print(a+b)
You cannot add a string and a number together, otherwise what would you get? If you really want to do the "addition", there is a solution:
a ="Tom"
b ="2"print(a+b)
What you will get is a combination of two strings, in their sequence of addition, which is "Tom2".
Besides, if you would like to add a number with a "string number" (a string which contains ONLY numbers), there is a way to do it:
a ="100"
b =133print(int(a) + 133)
You will get 233 as a result obviously. I have also discovered that "int()" can convert a decimal number to a whole number, throwing its decimal place away. For example, the result of both int(1.4) and int(1.6) are 1.
The third type of errors are called semantic errors. They are very hard to detect and fix, at least the Python interpreter will not return an error. You will not get an ideal result or a correct result. From what I have learned today and my early programming experience, semantic errors maybe caused by mistaking the open interval and closed interval, incorrect exit condition of repetition, neglect of resetting variables and deficiency in algorithms. For simplification, I will only show the example that our teacher gave in class.
Result =0for i inrange(1,10):
Result = Result + i
print(Result)
This is the program ideally designed to sum up integers from 1 to 10 and get the result. However, the programmer forgot that the number on the right side of the comma in the parenthesis should be the closed interval, so the i will stop increasing when it reaches 9. As the result he will get 45 instead of 55.
Other details of Python that we have come across today:
Problems of quotation marks: There are three kinds of quotation marks that are used to hold strings. They are single quotation mark, double quotation marks and triple quotation marks. They have no difference normally, but you may need to use them wisely when you come across a situation in which quotation marks within the quotation marks are necessary. For instance:
print('''"Tom is tricky", Doris said''')
It will not be a problem as you use only one quotation mark rather than a pair of quotation marks. Use apostrophe as an example:
print("Tom's hair style looks cool")
When you type the apostrophe, you are actually typing a quotation mark. You can try it out in Python shell and see if it will affect the result or not.
You can actually use any combination of any two kinds of quotation marks when you need them, and you can even use them all.
Unlike some other programming languages such as VB or C++, you do not need to declare the data types of variables before using them in Python. In addition, the data types of variables can be altered during operations, as you may assign a different type of data to it. For example:
a ="Nice day today"
a =666print(a)
The original data type of the variable "a" is string, then it is changed to integer. The final output of the program is the integer 666.
You can think of more if you can recall from the class or if you want. :)
Wednesday (June 1st)
Agenda:
Homework:
Exercises from chapter 1&2 in (Think like a computer scientist)Submit your code to project
Learning outcomes -- Edited by Tom Zhou on June 1st
----About half of the things that we learned today are referred to the chapter one of the book "How to think like a computer scientist". To find out more, click me to see that chapter.
WikiSpace is the place where you show the result of your study, works, projects and activities. Everyone in our class can be the editor, and your efforts will make it more diverse and appealing to others.
To be a good computer scientist, you should:
Also we should be aware of the term "open source", which means that the code of the programes is published to all programmers in the world and you can change any part of it if you want. A significant benefit of open source is that others will not only have the chance of learning your code and algorithms, but also they can improve it bit by bit. In the class, we had better share our resources and workings in order to improve our abilities together.
The key things we learned today of programming may include:
--Introduction to Python as a programming language
--Definition of debugging and three errors that may occur
--Simple grammars involved in Python programming
--Some useful functions and modules in Python
As beginners, we should know that Python is an interpreted language. It is interpreted line by line, to a language that computers can understand and it runs immediately. By comparison, compiled languages such as VB and C++ will be only translated as a whole and it will not run right after translation.
There are three general types of errors that may occur during debugging. The first one is called “Syntax error”, which is resulted from incorrect notations or grammar. For example:
If you execute the program, it is very likely to return an error simply because you missed the parentheses. The correct format should be:
Also please notice that the uppercase letters are very DIFFERENT from lowercase letters. Your Python interpreter will not recognize them if you type the word "Print" rather than the word "print".
The second type of errors are runtime errors. Their typical examples are the inappropriate operations between data types. For instance:
You cannot add a string and a number together, otherwise what would you get? If you really want to do the "addition", there is a solution:
What you will get is a combination of two strings, in their sequence of addition, which is "Tom2".
Besides, if you would like to add a number with a "string number" (a string which contains ONLY numbers), there is a way to do it:
You will get 233 as a result obviously. I have also discovered that "int()" can convert a decimal number to a whole number, throwing its decimal place away. For example, the result of both int(1.4) and int(1.6) are 1.
The third type of errors are called semantic errors. They are very hard to detect and fix, at least the Python interpreter will not return an error. You will not get an ideal result or a correct result. From what I have learned today and my early programming experience, semantic errors maybe caused by mistaking the open interval and closed interval, incorrect exit condition of repetition, neglect of resetting variables and deficiency in algorithms. For simplification, I will only show the example that our teacher gave in class.
This is the program ideally designed to sum up integers from 1 to 10 and get the result. However, the programmer forgot that the number on the right side of the comma in the parenthesis should be the closed interval, so the i will stop increasing when it reaches 9. As the result he will get 45 instead of 55.
Other details of Python that we have come across today:
Problems of quotation marks: There are three kinds of quotation marks that are used to hold strings. They are single quotation mark, double quotation marks and triple quotation marks. They have no difference normally, but you may need to use them wisely when you come across a situation in which quotation marks within the quotation marks are necessary. For instance:
It will not be a problem as you use only one quotation mark rather than a pair of quotation marks. Use apostrophe as an example:
When you type the apostrophe, you are actually typing a quotation mark. You can try it out in Python shell and see if it will affect the result or not.
You can actually use any combination of any two kinds of quotation marks when you need them, and you can even use them all.
Unlike some other programming languages such as VB or C++, you do not need to declare the data types of variables before using them in Python. In addition, the data types of variables can be altered during operations, as you may assign a different type of data to it. For example:
The original data type of the variable "a" is string, then it is changed to integer. The final output of the program is the integer 666.
You can think of more if you can recall from the class or if you want. :)